<?php
session_start();

$public_id="gh_82e0115171fc";//公众号原始id
$api_key="St3No2CT";//api_key

//appid&secret
$data = curl_post("http://wx.brandsh.cn/index.php?s=/addon/Post/Post/index.html&type=appid", array('public_id'=>$public_id,'api_key'=>$api_key));    
//echo $data;
$dataObj=json_decode($data);

//Access_token
$Access_token=curl_post("http://wx.brandsh.cn/index.php?s=/addon/Post/Post/index.html&type=access_token",array('public_id'=>$public_id,'api_key'=>$api_key,'appid'=>$dataObj->appid,'secret'=>$dataObj->secret));
//echo $Access_token; 

// 调用分享接口
$url = "http://wx.brandsh.cn/index.php?s=/addon/Post/Post/index.html&type=getShare";
$sharePost=array(
    'public_id'=>$public_id,
    'api_key'=>$api_key,
    'url'=>'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']  //当前url
);
$signPackage = curl_post($url,$sharePost);
$signPackage =json_decode($signPackage,true);


//openid获取
//用于清空cookie openid
//setcookie('openid', "");
//setcookie('getOpenid', "");
    $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//获取当前URL
        if($_COOKIE['openid'] =="" || $_COOKIE['openid'] ==NULL){
            if($_GET ['openid']=="" || $_COOKIE['getOpenid'] !="true"){
                setcookie('getOpenid',"true",time()+7200);
                header("Location:"."http://wx.brandsh.cn/index.php?s=/addon/Post/Post/getopenid.html&type=openid&public_id=".$public_id."&api_key=".$api_key."&url=".urlencode($url)."&scope=0"); 
            }else{
                setcookie('openid', $_GET ['openid'],time()+7200);
                
                $openid=$_GET ['openid'];


                //&scope=1时可以获取到以下信息，获取到以下信息后可以记录在数据库中保存
                // echo $_GET ['nickname'];
                // echo $_GET ['sex'];
                // echo $_GET ['province'];
                // echo $_GET ['city'];
                // echo $_GET ['headimgurl'];
            }
        }else{
            $openid=$_COOKIE['openid'];
        }
if(isset($_SESSION['openid'])){
  //$_SESSION['openid']=$openid;
}else{
  $_SESSION['openid']=$openid;
}
// echo  $openid;
$doadmin = $_SERVER['HTTP_HOST'];
//奖品
$prize = httpGet("http://".$_SERVER['HTTP_HOST']."/wx/pearl/admin.php/api_port/stargame/".$openid);

//echo $prize;
function curl_post($url, $post) {  
    $options = array(  
        CURLOPT_RETURNTRANSFER => true,  
        CURLOPT_HEADER         => false,  
        CURLOPT_POST           => true,  
        CURLOPT_POSTFIELDS     => $post,  
    );  
  
    $ch = curl_init($url);  
    curl_setopt_array($ch, $options);  
    $result = curl_exec($ch);  
    curl_close($ch);  
    return $result;  
} 

function is_weixin(){ 
        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
        }   
    return false;
}

function checkmobile() {
 global $_G;
 $mobile = array();
//各个触控浏览器中$_SERVER['HTTP_USER_AGENT']所包含的字符串数组
 static $touchbrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
    'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
    'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
    'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
    'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
    'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
    'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
//window手机浏览器数组【猜的】
 static $mobilebrowser_list =array('windows phone');
//wap浏览器中$_SERVER['HTTP_USER_AGENT']所包含的字符串数组
 static $wmlbrowser_list = array('cect', 'compal', 'ctl', 'lg', 'nec', 'tcl', 'alcatel', 'ericsson', 'bird', 'daxian', 'dbtel', 'eastcom',
   'pantech', 'dopod', 'philips', 'haier', 'konka', 'kejian', 'lenovo', 'benq', 'mot', 'soutec', 'nokia', 'sagem', 'sgh',
   'sed', 'capitel', 'panasonic', 'sonyericsson', 'sharp', 'amoi', 'panda', 'zte');
 $pad_list = array('pad', 'gt-p1000');
 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
 if(dstrpos($useragent, $pad_list)) {
  return false;
 }
 if(($v = dstrpos($useragent, $mobilebrowser_list, true))){
  $_G['mobile'] = $v;
  return '1';
 }
 if(($v = dstrpos($useragent, $touchbrowser_list, true))){
  $_G['mobile'] = $v;
  return '2';
 }
 if(($v = dstrpos($useragent, $wmlbrowser_list))) {
  $_G['mobile'] = $v;
  return '3'; //wml版
 }
 $brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
 if(dstrpos($useragent, $brower)) return false;
 $_G['mobile'] = 'unknown';
//对于未知类型的浏览器，通过$_GET['mobile']参数来决定是否是手机浏览器
 if(isset($_G['mobiletpl'][$_GET['mobile']])) {
  return true;
 } else {
  return false;
 }
}
/**
 * 判断$arr中元素字符串是否有出现在$string中
 * @param  $string     $_SERVER['HTTP_USER_AGENT'] 
 * @param  $arr          各中浏览器$_SERVER['HTTP_USER_AGENT']中必定会包含的字符串
 * @param  $returnvalue 返回浏览器名称还是返回布尔值，true为返回浏览器名称，false为返回布尔值【默认】
 * @author           discuz3x
 * @lastmodify    2014-04-09
 */
function dstrpos($string, $arr, $returnvalue = false) {
 if(empty($string)) return false;
 foreach((array)$arr as $v) {
  if(strpos($string, $v) !== false) {
   $return = $returnvalue ? $v : true;
   return $return;
  }
 }
 return false;
}
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}



?>
